package com.ny.service.impl;

import com.ny.mapper.CourseReservationMapper;
import com.ny.mapper.GroupCourseInfoMapper;
import com.ny.mapper.MemberMapper;
import com.ny.mapper.PersonalTrainerCourseMapper;
import com.ny.domain.GroupCourseInfoDO;
import com.ny.domain.PersonalTrainerCourseDO;
import com.ny.dto.CourseReservationDTO;
import com.ny.dto.GroupCourseInfoDTO;
import com.ny.dto.PersonalTrainerCourseDTO;
import com.ny.query.GroupCourseInfoQuery;
import com.ny.query.PersonalTrainerCourseQuery;
import com.ny.service.CourseService;
import java.lang.Boolean;
import java.lang.Integer;
import java.lang.Override;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ny.vo.PrivateCourseVO;
import com.ruoyi.common.utils.DateTimeUtils;
import org.assertj.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ny.exception.BusinessException;
import com.ny.domain.CourseReservationDO;
import org.springframework.util.CollectionUtils;

/**
 * 课程管理模块的实现
 *
 * @author MSI-NB
 * @date 2025-03-08 13:14:38
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private CourseReservationMapper courseReservationMapper;

    @Autowired
    private PersonalTrainerCourseMapper personalTrainerCourseMapper;

    @Autowired
    private GroupCourseInfoMapper groupCourseInfoMapper;

    @Override
    public Boolean addPersonalTrainerCourse(PersonalTrainerCourseDTO personalTrainerCourseDTO) {
        // 验证教练ID是否存在
        if (memberMapper.selectById(personalTrainerCourseDTO.getTrainerId()) == null) {
            throw new BusinessException("教练ID不存在");
        }
        // 验证课程开始时间和结束时间是否冲突
        if (isCourseTimeConflict(personalTrainerCourseDTO.getTrainerId(), personalTrainerCourseDTO.getCourseStartTime(), personalTrainerCourseDTO.getCourseEndTime())) {
            throw new BusinessException("课程时间冲突，请重新选择时间");
        }
        // 保存私教课程信息
        PersonalTrainerCourseDO personalTrainerCourseDO = new PersonalTrainerCourseDO();
        personalTrainerCourseDO.setTrainerId(personalTrainerCourseDTO.getTrainerId());
        personalTrainerCourseDO.setCourseName(personalTrainerCourseDTO.getCourseName());
        personalTrainerCourseDO.setCourseDescription(personalTrainerCourseDTO.getCourseDescription());
        personalTrainerCourseDO.setCourseStartTime(personalTrainerCourseDTO.getCourseStartTime());
        personalTrainerCourseDO.setCourseEndTime(personalTrainerCourseDTO.getCourseEndTime());
        int result = personalTrainerCourseMapper.insert(personalTrainerCourseDO);
        return result > 0;
    }

    @Override
    public Boolean addGroupCourseInfo(GroupCourseInfoDTO groupCourseInfoDTO) {
        // 验证课程开始时间和结束时间是否冲突
        if (isCourseTimeConflict(groupCourseInfoDTO.getCourseStartTime(), groupCourseInfoDTO.getCourseEndTime())) {
            throw new BusinessException("课程时间冲突，请重新选择时间");
        }
        // 保存团队课程信息
        GroupCourseInfoDO groupCourseInfoDO = new GroupCourseInfoDO();
        groupCourseInfoDO.setCourseName(groupCourseInfoDTO.getCourseName());
        groupCourseInfoDO.setCourseDescription(groupCourseInfoDTO.getCourseDescription());
        groupCourseInfoDO.setCourseStartTime(groupCourseInfoDTO.getCourseStartTime());
        groupCourseInfoDO.setCourseEndTime(groupCourseInfoDTO.getCourseEndTime());
        int result = groupCourseInfoMapper.insert(groupCourseInfoDO);
        return result > 0;
    }

    @Override
    public List<PrivateCourseVO> getPersonalTrainerCourseInfo(PersonalTrainerCourseQuery personalTrainerCourseQuery) {
        List<PersonalTrainerCourseDO> courses = personalTrainerCourseMapper.selectByTrainerIdAndName(personalTrainerCourseQuery.getTrainerId(), personalTrainerCourseQuery.getCourseName());
        if (CollectionUtils.isEmpty(courses)) {
            courses = new ArrayList<>();
            // 设置mock数据
            PersonalTrainerCourseDO personalTrainerCourseDO = new PersonalTrainerCourseDO();
            // 设置模拟参数
            personalTrainerCourseDO.setCourseId(1);                            // 模拟课程ID
            personalTrainerCourseDO.setTrainerId(100);                         // 模拟教练ID
            personalTrainerCourseDO.setCourseName("私教体验课程");               // 模拟课程名称
            personalTrainerCourseDO.setCourseDescription("首次私教课程体验，包含体测和基础训练"); // 模拟课程描述

            Date now = new Date();
            personalTrainerCourseDO.setCourseStartTime(now);                    // 开始时间设为当前时间

            // 结束时间设为1小时后（3600秒 * 1000毫秒）
            personalTrainerCourseDO.setCourseEndTime(new Date(now.getTime() + 3600 * 1000));

            personalTrainerCourseDO.setCreateBy("system");                     // 创建人
            personalTrainerCourseDO.setCreateTime(now);                        // 创建时间
            personalTrainerCourseDO.setUpdateBy("system");                     // 修改人
            personalTrainerCourseDO.setUpdateTime(now);                        // 修改时间
            courses.add(personalTrainerCourseDO);

        }


        List<PrivateCourseVO> collect = courses.stream().map(course -> {
            PrivateCourseVO privateCourseVO = new PrivateCourseVO();
            privateCourseVO.setCourseName(course.getCourseName());
            privateCourseVO.setCourseContent(course.getCourseDescription());
            privateCourseVO.setCourseStartTime(DateTimeUtils.dateToLocalDateTime(course.getCourseStartTime()));
            privateCourseVO.setCoachId(course.getTrainerId());

            return privateCourseVO;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<GroupCourseInfoDO> getGroupCourseInfo(GroupCourseInfoQuery groupCourseInfoQuery) {
        List<GroupCourseInfoDO> courses = groupCourseInfoMapper.selectByName(groupCourseInfoQuery.getCourseName());
        if (CollectionUtils.isEmpty(courses)){
            GroupCourseInfoDO groupCourseInfoDO = new GroupCourseInfoDO();
            groupCourseInfoDO.setCourseId(1);
            groupCourseInfoDO.setCourseName("瑜伽");
            groupCourseInfoDO.setCourseDescription("瑜伽");
            groupCourseInfoDO.setCourseStartTime(new Date());
            groupCourseInfoDO.setCourseEndTime(new Date());
            groupCourseInfoDO.setCreateBy("admin");
            groupCourseInfoDO.setCreateTime(new Date());
            groupCourseInfoDO.setUpdateBy("admin");
            groupCourseInfoDO.setUpdateTime(new Date());
            courses.add(groupCourseInfoDO);
        }

        return courses;
    }

    @Override
    public Boolean reservePersonalTrainerCourse(CourseReservationDTO courseReservationDTO) {
        // 验证会员ID是否存在
        if (memberMapper.selectById(courseReservationDTO.getMemberId()) == null) {
            throw new BusinessException("教练ID不存在");
        }
        // 验证课程ID是否存在
        if (personalTrainerCourseMapper.selectByTrainerIdAndName(courseReservationDTO.getCourseId(), null) == null) {
            throw new BusinessException("课程ID不存在");
        }
        // 验证会员是否已有该课程的预约记录
        if (courseReservationMapper.selectByMemberIdAndCourseId(courseReservationDTO.getMemberId(), courseReservationDTO.getCourseId()) != null) {
            throw new BusinessException("教练ID不存在");
        }
        // 验证课程是否已满员
        if (isCourseFull(courseReservationDTO.getCourseId())) {
            throw new BusinessException("教练ID不存在");
        }
        // 保存预约记录
        CourseReservationDO courseReservationDO = new CourseReservationDO();
        courseReservationDO.setMemberId(courseReservationDTO.getMemberId());
        courseReservationDO.setCourseId(courseReservationDTO.getCourseId());
        courseReservationDO.setReservationDate(new Date());
        int result = courseReservationMapper.insert(courseReservationDO);
        return result > 0;
    }

    @Override
    public Boolean reserveGroupCourse(CourseReservationDTO courseReservationDTO) {
        // 验证会员ID是否存在
        if (memberMapper.selectById(courseReservationDTO.getMemberId()) == null) {
            throw new BusinessException("教练ID不存在");
        }
        // 验证课程ID是否存在
        if (groupCourseInfoMapper.selectById(courseReservationDTO.getCourseId()) == null) {
            throw new BusinessException("课程ID不存在");
        }
        // 验证会员是否已有该课程的预约记录
        if (courseReservationMapper.selectByMemberIdAndCourseId(courseReservationDTO.getMemberId(), courseReservationDTO.getCourseId()) != null) {
            throw new BusinessException("教练ID不存在");
        }
        // 验证课程是否已满员
        if (isCourseFull(courseReservationDTO.getCourseId())) {
            throw new BusinessException("课程满员");
        }
        // 保存预约记录
        CourseReservationDO courseReservationDO = new CourseReservationDO();
        courseReservationDO.setMemberId(courseReservationDTO.getMemberId());
        courseReservationDO.setCourseId(courseReservationDTO.getCourseId());
        courseReservationDO.setReservationDate(new Date());
        int result = courseReservationMapper.insert(courseReservationDO);
        return result > 0;
    }

    @Override
    public Boolean cancelPersonalTrainerCourseReservation(CourseReservationDTO courseReservationDTO) {
        // 验证会员ID是否存在
        if (memberMapper.selectById(courseReservationDTO.getMemberId()) == null) {
            throw new BusinessException("会员ID不存在");
        }
        // 验证预约ID是否存在
        if (courseReservationMapper.selectById(courseReservationDTO.getReservationId()) == null) {
            throw new BusinessException("预约ID不存在");
        }
        // 删除预约记录
        int result = courseReservationMapper.deleteById(courseReservationDTO.getReservationId());
        return result > 0;
    }

    @Override
    public Boolean cancelGroupCourseReservation(CourseReservationDTO courseReservationDTO) {
        // 验证会员ID是否存在
        if (memberMapper.selectById(courseReservationDTO.getMemberId()) == null) {
            throw new BusinessException("会员ID不存在");
        }
        // 验证预约ID是否存在
        if (courseReservationMapper.selectById(courseReservationDTO.getReservationId()) == null) {
            throw new BusinessException("预约ID不存在");
        }
        // 删除预约记录
        int result = courseReservationMapper.deleteById(courseReservationDTO.getReservationId());
        return result > 0;
    }

    private Boolean isCourseTimeConflict(Integer trainerId, Date courseStartTime, Date courseEndTime) {
        return personalTrainerCourseMapper.countByTrainerIdAndTime(trainerId, courseStartTime, courseEndTime) > 0;
    }

    private Boolean isCourseTimeConflict(Date courseStartTime, Date courseEndTime) {
        return groupCourseInfoMapper.countByTime(courseStartTime, courseEndTime) > 0;
    }

    private Boolean isCourseFull(Integer courseId) {
        // 假设最大容量为10
        int maxCapacity = 10;
        return courseReservationMapper.countByCourseId(courseId) >= maxCapacity;
    }
}
